/*-//GEN-BEGIN:copyright
 * ForBILE - Framework for Building Interactive Learning Environments
 * Computing Institute - Federal University of Alagoas - Brazil
 * Group of Otimization of the Web
 *
 * Copyright (C) 2007 Ig Bittencourt (ig.ibert@gmail.com)
 *		      Camila Nunes (camilapbnunes@hotmail.com)
 *	              Marcos Tadeu (zero.ufal@gmail.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *///GEN-END:copyright
package br.ufal.ic.forbile.agents.controller.behaviours;

import br.ufal.ic.forbile.agents.ForBILEAgent;
import br.ufal.ic.forbile.agents.behaviours.GeneralSensor;
import br.ufal.ic.forbile.agents.controller.ControllerAgent;
import br.ufal.ic.forbile.exceptions.ForBILEException;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.UnreadableException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
 * SensorController.java
 *
 * <p>A full description of this class.
 *
 * @see SomeRelatedClass.
 *
 * @author <a href="mailto:camilapbnunes@hotmail.com">camila</a>.
 * @since 0.1
 * @version 0.1
 *
 */
public class SensorController extends GeneralSensor{
    
    // Constants -----------------------------------------------------
    
    // Attributes ----------------------------------------------------
    
    // Static --------------------------------------------------------
    
    private static Logger logger;
    
    static {
        logger = Logger.getLogger(SensorController.class.getName());
        
    }
    
    // Constructors --------------------------------------------------
    
    
    /**
     * Creates a new instance of SensorController
     */
    public SensorController(final ForBILEAgent agent, final long time) {
        super(agent, time);
    }
    
    // Public --------------------------------------------------------
    
    // Z implementation ----------------------------------------------
    
    // Y overrides ---------------------------------------------------
    
    // Package protected ---------------------------------------------
    
    // Protected -----------------------------------------------------
    
    protected void callBehaviour(final ACLMessage message) {
        
        if (message.getInReplyTo() != null) {
            if (message.getInReplyTo().toUpperCase().equals("START")) {
                try {
                    ( (ControllerAgent) myAgent).agentAdded(message.getSender(), true);
                } catch (ForBILEException ex) {
                    new ForBILEException("AgentSociety could not be added", ex);
                }
            }
            
            if (message.getInReplyTo().equalsIgnoreCase("O2ACommunication")) {
                ( (ControllerAgent) myAgent).a2oCommunicationReturn(null);
            }
        }
        
        if (message.getProtocol() != null) {
            if (message.getProtocol().toUpperCase().equals("ADDAGENT")) {
                ForBILEAgent forBILEAgent = null;
                try {
                    forBILEAgent = (ForBILEAgent) message.getContentObject();
                    ( (ControllerAgent) myAgent).addAgent(forBILEAgent);
                } catch (UnreadableException ex) {
                    new ForBILEException("AgentSociety could not be added", ex);
                }  catch (ForBILEException ex) {
                    new ForBILEException("AgentSociety could not be added", ex);
                }
            }
            
            if (message.getProtocol().toUpperCase().equals("ADDAGENTSOCIETY")) {
                List<ForBILEAgent> forBILEAgents = new ArrayList<ForBILEAgent>();
                try {
                    forBILEAgents = (ArrayList<ForBILEAgent>) message.getContentObject();
                    ( (ControllerAgent) myAgent).addAgentSociety(forBILEAgents);
                } catch (UnreadableException ex) {
                    new ForBILEException("AgentSociety could not be added", ex);
                } catch (ForBILEException ex) {
                    new ForBILEException("AgentSociety could not be added", ex);
                }
            }
        }
    }
    // Private -------------------------------------------------------
    
    // Inner classes -------------------------------------------------
    
    
}
